//
//  AFNETWORK.m
//  PeaceComputer
//
//  Created by dlios on 15-9-2.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AFNETWORK.h"
#import "AFNetworking.h"
@implementation AFNETWORK
#pragma mark - 刷新缓存
+ (void)getDataWithString:(NSString *)string block:(AFNData)blockData
{
    NSString *key = [URLCache md5:string];
    NSData *data = [URLCache objectForKey:key];
    if (data) {
        NSDictionary *dic = [URLCache returnDictionaryWithDataPath:data];
        blockData([NSDictionary dictionaryWithDictionary:dic]);
    }
    AFNETWORK *netWork = [[AFNETWORK alloc] init];
    [netWork getdataWithString:string block:^(id block) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:block options:NSJSONWritingPrettyPrinted error:&error];
        [URLCache removeObjectForkey:key];
        [URLCache setObject:jsonData forKey:key];
        blockData(block);
    }];
}
#pragma mark - 不读取缓存只刷新
+ (void)reloadDataWithString:(NSString *)string block:(AFNData)blockNewData
{
    NSString *key = [URLCache md5:string];
    AFNETWORK *netWork = [[AFNETWORK alloc] init];
    [netWork getdataWithString:string block:^(id block) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:block options:NSJSONWritingPrettyPrinted error:&error];
        [URLCache removeObjectForkey:key];
        [URLCache setObject:jsonData forKey:key];
        blockNewData(block);
    }];
}
#pragma mark - 不刷新缓存
+ (void)reloadDataUpWithString:(NSString *)string block:(AFNData)blockNewDataUp
{
    AFNETWORK *netWork = [[AFNETWORK alloc] init];
    [netWork getdataWithString:string block:^(id block) {
        blockNewDataUp(block);
    }];
}
#pragma mark - 私有方法
- (void)getdataWithString:(NSString *)string block:(AFNData)block
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"Error: %@", error);
        NSLog(@"没网=========");
    }];
}
#pragma mark - 不带缓存操作
+ (void)getDataWithNoCacheString:(NSString *)string block:(AFNData)block
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Error: %@", error);
        NSLog(@"没网=========");
    }];
}
@end
